package com.explorati.design_pattern.creational.singleton;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

public class Test {

	public static void main(String[] args)
			throws FileNotFoundException, IOException, ClassNotFoundException, NoSuchMethodException, SecurityException,
			InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
		// LazySingleton lazySigleton = LazySingleton.getInstance();
		// System.out.println("program end");

		// new两个线程，人为debug干预执行顺序，可得两个对象可以一样，也可不一样
		/**
		 * 当同时执行到null中时，对象不一样 一个先new完之后，是一个对象
		 */
		// Thread T1 = new Thread(new T());
		// Thread T2 = new Thread(new T());
		// T1.start();
		// T2.start();
		// System.out.println("program end");

		// 序列化破坏单例模式测试
		// HungrySingleton instance = HungrySingleton.getInstance();
		// EnumInstance instance = EnumInstance.getInstance();
		// instance.setData(new Object());
		//
		// ObjectOutputStream oos = new ObjectOutputStream(new
		// FileOutputStream("singleton_file"));
		// oos.writeObject(instance);
		//
		// File file = new File("singleton_file");
		// ObjectInputStream ois = new ObjectInputStream(new
		// FileInputStream(file));
		//
		// EnumInstance newInstance = (EnumInstance) ois.readObject();
		// // HungrySingleton newInstance = (HungrySingleton) ois.readObject();
		//
		// System.out.println(instance.getData());
		// System.out.println(newInstance.getData());
		// System.out.println(instance.getData() == newInstance.getData());

		// Class objectClass = HungrySingleton.class;
		// Class objectClass = StaticInnerClassSingleton.class;
		// Constructor constructor = objectClass.getDeclaredConstructor();
		// // 私有方法必须setAccessible(true)
		// constructor.setAccessible(true);
		// StaticInnerClassSingleton instance =
		// StaticInnerClassSingleton.getInstance();
		// StaticInnerClassSingleton newInstance = (StaticInnerClassSingleton)
		// constructor.newInstance();
		// // HungrySingleton instance = HungrySingleton.getInstance();
		// // HungrySingleton newInstance = (HungrySingleton)
		// // constructor.newInstance();
		//
		// System.out.println(instance);
		// System.out.println(newInstance);
		// System.out.println(instance == newInstance);

//		EnumInstance instance = EnumInstance.getInstance();
//		instance.printTest();
		
		Thread t = new Thread(new T());
		Thread t1 = new Thread(new T());
		 t.start();
		 t1.start();
		 System.out.println("program end");
	}
}
